//
//  ConsultHistoryCtr-cell.swift
//  smosky
//
//  Created by zhuxietong on 2018/8/8.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import UIKit
import Eelay
import soapp
import AsyncDisplayKit
extension ConsultHistoryCtr {
    
    enum PopType:String{
        case me = "me"
        case ta = "ta"
        
        var img:UIImage{
            get{
                switch self {
                case .me:
                    let img = UIImage(named: "chatpop_me")!
                    return img.resizableImage(withCapInsets: [24,20,8,20], resizingMode: .stretch)
                    
                default:
                    let img = UIImage(named: "chatpop_ta")!
                    return img.resizableImage(withCapInsets: [24,20,8,20], resizingMode: .stretch)
                }
            }
        }
        
        var contentColor:UIColor{
            get{
                switch self {
                case .me:
                    return ._foreground
                default:
                    return .white
                }
            }
        }
        
        var popInset:UIEdgeInsets{
            get{
                switch self {
                case .me:
                    return [-2,-2,-2,-8]
                default:
                    return [-2,-8,-2,-2]
                }
            }
        }
    }
    
    class ChatPopCell: JoCellNode {
        let titleL = ASTextNode2()
        let infoL = ASTextNode2()
        
        let line = ASDisplayNode()
        let popV = ASImageNode()
        
        
        let timeL = ASTextNode2()
        let userL = ASTextNode2()
        var popType:PopType = .me
        
        var actionBt = ASButtonNode()
        
        
        var buttonName = ""
        override func addLayoutRules() {
            backgroundColor = .clear
            actionBt.addTarget(self, action: #selector(buttonAction), forControlEvents: .touchUpInside)
        }
        
        @objc func buttonAction(sender:ASButtonNode){
//            print("+++++--|\(mo.model)",sender.actionID)
            AppLink.open(link: mo.model["link",""], at: mo.controller)
//            switch sender.actionID {
//            case "查看物流":
//
//            default:
//                break
//            }
//            print("=====iiiii",sender.actionID);
        }
        
        override func mo_model_reload() {
            popType = PopType(rawValue: mo.model["cellID","ta"]) ?? .ta

            titleL.attributedText = mo.model["title",""].ui.font15.lineSpace(6).color(color: popType.contentColor).astring
            infoL.attributedText = mo.model["content",""].ui.font14.lineSpace(6).color(color: popType.contentColor).astring
            timeL.attributedText = stampToTime(mo.model["createtime",""])["yyyy-MM-dd HH:mm"].ui.center.font11.cl_888.astring
            userL.attributedText = " ".ui.font11.cl_888.astring + mo.model["operator","店小二".loc].ui.font11.cl_888.astring

            line.backgroundColor = ._separator
            popV.image = popType.img
            
            if mo.model["link",""].hasPrefix("expresslist")
            {
                buttonName = "查看物流".loc
                actionBt.ui.font(size: 12).ntitle(title: buttonName).padding(insets:[6,16]).ncolor(color: popType.contentColor).set(radius: 3, width: 1, color: popType.contentColor)
            }
            else{
                buttonName = ""
            }
     
        }
        
        func contentSpec() -> _Spec {

            var items:[_Spec] = [
                titleL.Spec,
                line["\(TP.separatorHeight)"],
                infoL.Spec
            ]
            if buttonName.len > 0
            {
                items.append(.stack(["h",_justfy(.flex_end)], childs: [
                    actionBt.Spec
                    ]))
            }
            let content:_Spec = _Spec.stack(["v|8",&-.stretch], childs: items)
            
            let pop = _Spec.back([], over: .inset([], [10], child: content), back: .inset([], popType.popInset, child: popV.Spec))
            return pop
        }
        
        
        override func specGetter() -> _Spec {
            
            var specs:[_Spec] = [_Spec]()
            switch popType {
            case .me:
                specs.append(timeL[[12,0,3,0]])
            default:
                specs.append(timeL[[12,0,-6,0]])
                specs.append(userL.Spec)
            }
            specs.append(contentSpec())
            
            return _Spec.stack(["v|6",&-.stretch,[10,AppTheme.edge]], childs:specs)
        }
        
    }
}
